<?php

class Admin_model extends CI_Model{

	function login($data)
	{
		return $this->db->get_where('users',$data)->row_array();
	}

	function add_slider_images($data,$files)
	{	
		$this->db->insert('slider',$data); 
        $id = $this->db->insert_id();

        if($files['name'] != '')
        {
            $logo_name_ext = pathinfo($files['name'], PATHINFO_EXTENSION);

            if(move_uploaded_file($files["tmp_name"], FCPATH."resources/images/slider_images/".$id.'.'.$logo_name_ext))
                $data['image_name'] = $id.'.'.$logo_name_ext;
        }
        return $this->db->update('slider',$data,array('id' => $id));
	}

	function get_slider_data()
	{
		return $this->db->get_where('slider')->result_array();
	}

	function get_slider_info($id)
	{
		return $this->db->get_where('slider',array('id' => $id))->row_array();
	}

	function del_slider($id)
	{
		return $this->db->delete('slider',array('id' => $id));
	}

	function update_slider($data,$files,$id)
	{
		$result = $this->db->get_where('slider', array('id' => $id))->row_array();   
        if($files['name'] != '')
        {
            $filename = FCPATH."resources/images/slider_images/".$result['image_name'];
            if(file_exists($filename))
                unlink($filename);
            $logo_name_ext = pathinfo($files['name'], PATHINFO_EXTENSION);

            if(move_uploaded_file($files["tmp_name"], FCPATH."resources/images/slider_images/".$id.'.'.$logo_name_ext))
                $data['image_name'] = $id.'.'.$logo_name_ext;
        }
        return $this->db->update('slider',$data,array('id' => $id));
	}

	function simple_upload($filename,$name,$type,$tmp_name,$size,$error)
     { 
         $allowedExts = array("png","jpg","jpeg");
         $temp = explode(".", $name);
         $extension = end($temp);
         if (in_array($extension, $allowedExts))
           {
           if ($error > 0)
             {
                 //return "Return Code: " . $_FILES["file"]["error"] . "<br>";exit;
                 return FALSE;
             }
           else
             {
              
             if (file_exists("./uploads/".$filename ))
               {
               		return  $name . " already exists. ";
               }
             else
               {	
               		//echo $tmp_name;exit;
	                move_uploaded_file($tmp_name,"./uploads/" . $filename);
	             	return $filename;
               }
             }
           }
         else
           {
             	return FALSE;
           }
     }
}